clear
capture restore
set scheme cleanplots, perm

* SET DIRECTORIES
global home "..."

global data "$home/Data/"
global output "$home/Output/"

cd $home

*SET CONTROL VARIABLES
local firm_controls "lev_d2c_1lag_std log_salesg_real_1lag_std size_assets_1lag_std ln_market_cap_q_1lag_std liquidity_1lag_std pcmq_1lag_std recpay2yq_1lag_std d2aq_1lag_std  tobins_q_1lag_std age_std dd_lag_std i.sector i.fqtr"

*************
***Options***
*************
*Set ivol measure to use as baseline
local matur m1545
*Set mp shock to use as baseline
local mps ff4

local mps_ints "c.`mps'#c.lev_d2c_1lag_std c.`mps'#c.log_salesg_real_1lag_std c.`mps'#c.size_assets_1lag_std c.`mps'#c.ln_market_cap_q_1lag_std c.`mps'#c.liquidity_1lag_std c.`mps'#c.pcmq_1lag_std c.`mps'#c.recpay2yq_1lag_std c.`mps'#c.d2aq_1lag_std c.`mps'#c.tobins_q_1lag_std c.`mps'#c.age_std c.`mps'#c.dd_lag_std"

********************************************************************************
********************************************************************************
********************************************************************************
*Read-in ivol data (contains all trading days for Compustat firms)
use "${data}final_daily_data", clear

*Keep pre-crisis sample only
keep if pre_crisis & fomc

gen fyear = year

gen sic_fire 	 	 = 0 if sic_2dig!=.
replace sic_fire 	 = 1 if sic_2dig>=60 & sic_2dig<=67

sort cusip_8 date

merge m:1 cusip month year using "${data}distance2default", keepusing(cusip year month dd dd_lag)
drop if _merge==2
drop _merge

*Set sample
*Only 1-day lagged value of ivol
keep if gap_dist_`matur'==0
*Drop outliers
drop if divol_`matur'_outlier

***Standardize quarterly Compustat variables and lagged ivol level
foreach i in log_salesg_real_1lag size_assets_1lag liquidity_1lag pcmq_1lag recpay2yq_1lag d2aq_1lag ln_market_cap_q_1lag tobins_q_1lag lev_d2c_1lag ivol_`matur'_lag dd_lag age {

	*Pre-Crisis standardization
	sum `i'
	gen `i'_std = (`i' - r(mean)) / r(sd)
	
}

*Standardize ivol and dd
_pctile ivol_`matur'_lag, percentiles(30 70)
gen ivol_`matur'_hi2lo = (ivol_`matur'_lag - r(r1)) / (r(r2) -  r(r1))

_pctile dd_lag, percentiles(30 70)
gen dd_hi2lo = (dd_lag - r(r1)) / (r(r2) -  r(r1))

*Standardize mps to unit standard deviation & positive is expansionary
egen date_tag=tag(date)

sum `mps' if pre_crisis & date_tag
replace `mps' = -`mps'/r(sd)

quietly: reghdfe firm_stock_chg `mps' ivol_`matur'_hi2lo c.`mps'#c.ivol_`matur'_hi2lo `firm_controls' if !unscheduled, a(cusip_8) cluster(cusip_8 date)
	outreg2 using "${output}table1.xml",  bdec(2) excel ctitle(stk_price) keep(`mps' c.`mps'#c.ivol_`matur'_hi2lo) replace

quietly: reghdfe firm_stock_chg `mps' ivol_`matur'_hi2lo c.`mps'#c.ivol_`matur'_hi2lo `firm_controls' c.`mps'#c.log_salesg_real_1lag_std if !unscheduled, a(cusip_8) cluster(cusip_8 date)
	outreg2 using "${output}table1.xml",  bdec(2) excel ctitle(stk_price) keep(`mps' c.`mps'#c.ivol_`matur'_hi2lo c.`mps'#c.log_salesg_real_1lag_std) append

quietly: reghdfe firm_stock_chg `mps' ivol_`matur'_hi2lo c.`mps'#c.ivol_`matur'_hi2lo `firm_controls' c.`mps'#c.size_assets_1lag_std if !unscheduled, a(cusip_8) cluster(cusip_8 date)
	outreg2 using "${output}table1.xml",  bdec(2) excel ctitle(stk_price) keep(`mps' c.`mps'#c.ivol_`matur'_hi2lo c.`mps'#c.size_assets_1lag_std) append
	
quietly: reghdfe firm_stock_chg `mps' ivol_`matur'_hi2lo c.`mps'#c.ivol_`matur'_hi2lo `firm_controls' c.`mps'#c.ln_market_cap_q_1lag_std if !unscheduled, a(cusip_8) cluster(cusip_8 date)
	outreg2 using "${output}table1.xml",  bdec(2) excel ctitle(stk_price) keep(`mps' c.`mps'#c.ivol_`matur'_hi2lo c.`mps'#c.ln_market_cap_q_1lag_std) append
	
quietly: reghdfe firm_stock_chg `mps' ivol_`matur'_hi2lo c.`mps'#c.ivol_`matur'_hi2lo `firm_controls' c.`mps'#c.liquidity_1lag_std if !unscheduled, a(cusip_8) cluster(cusip_8 date)
	outreg2 using "${output}table1.xml",  bdec(2) excel ctitle(stk_price) keep(`mps' c.`mps'#c.ivol_`matur'_hi2lo c.`mps'#c.liquidity_1lag_std) append
	
quietly: reghdfe firm_stock_chg `mps' ivol_`matur'_hi2lo c.`mps'#c.ivol_`matur'_hi2lo `firm_controls' c.`mps'#c.lev_d2c_1lag_std if !unscheduled, a(cusip_8) cluster(cusip_8 date)
	outreg2 using "${output}table1.xml",  bdec(2) excel ctitle(stk_price) keep(`mps' c.`mps'#c.ivol_`matur'_hi2lo c.`mps'#c.lev_d2c_1lag_std) append	
	
quietly: reghdfe firm_stock_chg `mps' ivol_`matur'_hi2lo c.`mps'#c.ivol_`matur'_hi2lo `firm_controls' c.`mps'#c.tobins_q_1lag_std if !unscheduled, a(cusip_8) cluster(cusip_8 date)
	outreg2 using "${output}table1.xml",  bdec(2) excel ctitle(stk_price) keep(`mps' c.`mps'#c.ivol_`matur'_hi2lo c.`mps'#c.tobins_q_1lag_std) append
	
quietly: reghdfe firm_stock_chg `mps' ivol_`matur'_hi2lo c.`mps'#c.ivol_`matur'_hi2lo `firm_controls' c.`mps'#c.age_std if !unscheduled, a(cusip_8) cluster(cusip_8 date)
	outreg2 using "${output}table1.xml",  bdec(2) excel ctitle(stk_price) keep(`mps' c.`mps'#c.ivol_`matur'_hi2lo c.`mps'#c.age_std) append
	
quietly: reghdfe firm_stock_chg `mps' ivol_`matur'_hi2lo c.`mps'#c.ivol_`matur'_hi2lo `firm_controls' c.`mps'#c.dd_lag_std if !unscheduled, a(cusip_8) cluster(cusip_8 date)
	outreg2 using "${output}table1.xml",  bdec(2) excel ctitle(stk_price) keep(`mps' c.`mps'#c.ivol_`matur'_hi2lo c.`mps'#c.dd_lag_std) append
		
quietly: reghdfe firm_stock_chg `mps' ivol_`matur'_hi2lo c.`mps'#c.ivol_`matur'_hi2lo `firm_controls' `mps_ints' if !unscheduled, a(cusip_8) cluster(cusip_8 date)
	outreg2 using "${output}table1.xml",  bdec(2) excel ctitle(stk_price) append
